// 加载http核心模块
import http from 'http'
import DataFormater from './middleware/DataFormater.js'
import createResponse from './middleware/response.js'
import requestStream from './utils/requestStream.js'
import './utils/prototypeExtend.js'

//创建web服务器
const server = http.createServer((req, res) => {
  req.pipe(DataFormater(req,res)).pipe(res)
  
  /* console.log(req.headers)
  const { url, method } = req
  if (url === '/test') {
    req.pipe(process.stdout)
    res.end('ok2')
  } else {
    req.pipe(DataFormater(req, res)).pipe(requestStream({
      hostname: 'localhost',
      port: 8080,
      method: 'POST',
      path: '/test',
      headers: {
        "Content-Type": 'application/x-www-form-urlencoded'
      }
    }))
    res.end('ok2')
  } */
  // sendFile({ url: 'http://localhost:8080/', fileName: 'fileData', data: { a: 1, b: 2 } })
  /* req.pipe(DataFormater()).pipe() */
  // res.end('ok')
})

//绑定端口号启动服务器
server.listen(8080, function () {
  console.log('服务器启动成功，可以通过"http://127.0.0.1:8080/"来进行访问');
});

process.on('uncaughtException', (err) => {
  console.error('全局异常捕获:', err);
});